#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int numDecodings(string s) {
        vector<int> dp(s.size());
        if (s[0] == '0') return 0;
        dp[0] = 1;

        for (int i = 1; i < s.size(); i++)
        {
            if (s[i] == '0')
            {
                if (s[i - 1] >= '3' || s[i - 1] == '0') return 0;
                if (i == 1) dp[i] = 1;
                else dp[i] = dp[i - 2];
            }
            else
            {
                dp[i] = dp[i - 1];
                if (s[i - 1] != '0')
                {
                    int num = (s[i - 1] - '0') * 10 + s[i] - '0';
                    if (num <= 26)
                    {
                        if (i == 1) dp[i] += 1;
                        else dp[i] += dp[i - 2];
                    }
                }
            }
        }
        return dp[s.size() - 1];
    }
};